package com.edu.activemq;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ScheduleMessageSenderService {
	
	private static Logger logger = Logger.getLogger(ScheduleMessageSenderService.class);
	
	@Resource(name = "jmsQueueTemplate")
	private JmsTemplate jmsQueueTemplate;
	
	/**
	 * 延时投递消息 
	* @param @param destination
	* @param @param message
	* @param @param delay  分 时 天 月 星期几 (40 1 * * *)
	* @return void  
	* @throws
	 */
	public void send(String destination, final Object message,final String delay) {
		jmsQueueTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				jmsQueueTemplate.convertAndSend(message, new ScheduleMessagePostProcessor(delay));
				logger.info("消息1：[" + message + "] 延时发送成功！");
				return null;
			}
		});
	}
	
	/**
	 * 延时发送
	* @param @param destination
	* @param @param message
	* @param @param delay 30 * 1000 (延时30秒)
	* @return void  
	* @throws
	 */
	public void send(String destination, final Object message,final Long delay){
		jmsQueueTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				jmsQueueTemplate.convertAndSend(message, new ScheduleMessagePostProcessor(delay));//30 * 1000 = 延时30秒
				logger.info("消息2：[" + message + "] 延时发送成功！");
				return null;
			}
		});
	}

}
